/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shoppingsystem.view.beans;

import java.awt.Cursor;
import java.io.Serializable;
import java.util.regex.PatternSyntaxException;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Victor Cifuentes (victor.cifuentes@correounivalle.edu.co)
 */
public class BuscadorTabla extends javax.swing.JPanel {

    private TableRowSorter<TableModel> filtro;
    private JTable tabla;

    /**
     * Creates new form BuscadorTabla
     */
    public BuscadorTabla() {
        initComponents();
        filtro = new TableRowSorter<>();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buscarLabel = new javax.swing.JLabel();
        buscarTextField = new javax.swing.JTextField();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        buscarLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shoppingsystem/icons/edit-find.png"))); // NOI18N
        buscarLabel.setLabelFor(buscarTextField);
        buscarLabel.setName("buscarLabel"); // NOI18N
        add(buscarLabel);

        buscarTextField.setText("Buscar...");
        buscarTextField.setName("buscarTextField"); // NOI18N
        buscarTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarTextFieldActionPerformed(evt);
            }
        });
        buscarTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                buscarTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                buscarTextFieldFocusLost(evt);
            }
        });
        add(buscarTextField);
    }// </editor-fold>//GEN-END:initComponents

    private void buscarTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarTextFieldActionPerformed
        this.getParent().setCursor(new Cursor(Cursor.WAIT_CURSOR));
        if (filtro.getModel() == null) {
            filtro.setModel(tabla.getModel());
        }
        if (tabla != null && tabla.getRowSorter() == null) {
            tabla.setRowSorter(filtro);
        }
        try {
            filtro.setRowFilter(RowFilter.regexFilter("(?i)"
                    + buscarTextField.getText().replace(" ", "\\s")));
        } catch (PatternSyntaxException pse) {
        }
        this.getParent().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_buscarTextFieldActionPerformed

    private void buscarTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_buscarTextFieldFocusGained
        buscarTextField.setText("");
    }//GEN-LAST:event_buscarTextFieldFocusGained

    private void buscarTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_buscarTextFieldFocusLost
        buscarTextField.setText("Buscar...");
    }//GEN-LAST:event_buscarTextFieldFocusLost

    public JTable getTabla() {
        return tabla;
    }

    public void setTabla(JTable tabla) {
        this.tabla = tabla;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel buscarLabel;
    private javax.swing.JTextField buscarTextField;
    // End of variables declaration//GEN-END:variables
}
